/*
 * @Description:
 * @Author: fhw
 * @Date: 2023-02-21 20:39:33
 */
import { ApiProperty } from '@nestjs/swagger';
import { BaseEntity, PaginatedResult } from 'src/common/entities/entities';
import { Status } from 'src/dictionaries/index.enum';
import { AdminUser } from 'src/modules/admin/admin-user/entities/admin-user.entity';
import {
  Entity,
  Column,
  OneToMany,
  JoinColumn,
  ManyToOne,
} from 'typeorm';
export enum RoleType {
  'ordinary' = 'ordinary',
  'super' = 'super',
}
export const RoleFieldName = {
  rolename: '角色名称',
  status: '是否启用',
  type: '角色类型',
  description: '描述',
  menu: '菜单',
  createById: '创建人',
}
@Entity({ name: 'role' })
export class RoleEntity extends BaseEntity {
  /** 角色名称 */
  @ApiProperty({ example: 'rolename', description: '角色名称' })
  @Column({ type: 'varchar', comment: '角色名称', length: 500 })
  rolename: string;

  /** 是否启用 */
  @ApiProperty({
    enum: Status,
    example: Status.ENABLED,
    description: '是否启用',
  })
  @Column({
    type: 'enum',
    enum: Status,
    default: Status.ENABLED,
  })
  status: Status;
  /** 角色类型 */
  @Column({
    type: 'enum',
    enum: RoleType,
    default: RoleType.ordinary,
  })
  type: RoleType;

  /** 描述 */
  @ApiProperty({ example: 'description', description: '描述' })
  @Column({ type: 'varchar', comment: '描述' })
  description: string;

  /** 菜单 */
  @ApiProperty({ example: 'menu', description: '菜单' })
  @Column({ type: 'json', comment: '菜单', nullable: true })
  menu: any;


  @OneToMany(() => AdminUser, (adminUser) => adminUser.roles)
  adminUsers: AdminUser[];

  /** 创建人 */
  @ApiProperty({ example: 'createById', description: '创建人' })
  @Column({ type: 'int', comment: '创建人' })
  createById: number;
  @ManyToOne(() => AdminUser, (adminUser) => adminUser.rolesCreateBy) // 指定另一面作为第二个参数
  @JoinColumn({ name: 'createById' })
  createBy: AdminUser;
}
export class RoleResult extends PaginatedResult {
  @ApiProperty({ example: 'data', description: 'data' })
  data: RoleEntity;
}
